package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.positiontype.BasePositionTypePageRequest;
import com.mes.cloud.api.dto.request.positiontype.BasePositionTypeRequest;
import com.mes.cloud.api.dto.request.positiontype.BatchEnableFlagRequest;
import com.mes.cloud.api.dto.response.positiontype.BasePositionTypeDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.BasePositionTypeDao;
import com.mes.cloud.entity.BasePositionType;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位类型
 *
 * @author wxm
 * @version 2019-08-08 15:16:52
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class BasePositionTypeService {

    private BasePositionTypeDao basePositionTypeDao;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public BasePositionTypeService(BasePositionTypeDao basePositionTypeDao, IPKeyGenerator iPKeyGenerator) {
        this.basePositionTypeDao = basePositionTypeDao;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param pageRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<BasePositionTypeDto>> pageList(BasePositionTypePageRequest pageRequest) {
        Page<BasePositionTypeDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        List<BasePositionType> basePositionTypeList = basePositionTypeDao.list(pageRequest);
        List<BasePositionTypeDto> basePositionTypeDtoList = new ArrayList<>();
        for (BasePositionType basePositionType : basePositionTypeList) {
            BasePositionTypeDto basePositionTypeDto = new BasePositionTypeDto();
            BeanUtils.copyProperties(basePositionType, basePositionTypeDto);
            basePositionTypeDtoList.add(basePositionTypeDto);
        }
        CommPager<BasePositionTypeDto> pageResult = new CommPager<>(page, basePositionTypeDtoList);
        JsonResult<CommPager<BasePositionTypeDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 查列表
     *
     * @param basePositionTypeRequest
     * @return JsonResult
     */
    public JsonResult<List<BasePositionTypeDto>> list(BasePositionTypeRequest basePositionTypeRequest) {
        //查询条件
        BasePositionType selectParam = new BasePositionType();
        BeanUtils.copyProperties(basePositionTypeRequest, selectParam);
        List<BasePositionType> basePositionTypeList = basePositionTypeDao.select(selectParam);
        List<BasePositionTypeDto> basePositionTypeDtoList = new ArrayList<>();
        for (BasePositionType basePositionType : basePositionTypeList) {
            BasePositionTypeDto basePositionTypeDto = new BasePositionTypeDto();
            BeanUtils.copyProperties(basePositionType, basePositionTypeDto);
            basePositionTypeDtoList.add(basePositionTypeDto);
        }
        JsonResult<List<BasePositionTypeDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(basePositionTypeDtoList);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param basePositionTypeRequest
     * @return JsonResult
     */
    public JsonResult insert(BasePositionTypeRequest basePositionTypeRequest) {
        JsonResult jsonResult = new JsonResult();
        BasePositionType basePositionType = new BasePositionType();
        BeanUtils.copyProperties(basePositionTypeRequest, basePositionType);
        basePositionType.setId(iPKeyGenerator.getGenerateKey());
        basePositionType.setUpdateDateStr(DateUtil.updateDateStr());
        basePositionType.setCreateBy(UserUtil.getCreateByName());
        basePositionType.setUpdateBy(UserUtil.getUpdateByName());
        basePositionType.setCreateByNo(UserUtil.getCreateByNo());
        basePositionType.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = basePositionTypeDao.insertSelective(basePositionType);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param basePositionTypeRequest
     * @return JsonResult
     */
    public JsonResult update(BasePositionTypeRequest basePositionTypeRequest) {
        BasePositionType basePositionType = new BasePositionType();
        BeanUtils.copyProperties(basePositionTypeRequest, basePositionType);
        basePositionType.setUpdateDateStr(DateUtil.updateDateStr());
        basePositionType.setUpdateBy(UserUtil.getUpdateByName());
        basePositionType.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = basePositionTypeDao.updateByPrimaryKeySelective(basePositionType);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 删除
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult delete(String id) {
        BasePositionType basePositionType = new BasePositionType();
        basePositionType.setId(id);
        basePositionType.setDelFlag(Constants.DEL_FLAG_YES);
        basePositionType.setUpdateDateStr(DateUtil.updateDateStr());
        basePositionType.setUpdateBy(UserUtil.getUpdateByName());
        basePositionType.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = basePositionTypeDao.updateByPrimaryKeySelective(basePositionType);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult get(String id) {
        JsonResult jsonResult = new JsonResult();
        BasePositionType basePositionType = basePositionTypeDao.selectByPrimaryKey(id);
        if (basePositionType == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        BasePositionTypeDto basePositionTypeDto = new BasePositionTypeDto();
        BeanUtils.copyProperties(basePositionType, basePositionTypeDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(basePositionTypeDto);
    }

    /**
     * 禁用启用
     *
     * @param id
     * @param enableFlag
     * @return JsonResult
     */
    public JsonResult updateEnableFlag(String id, String enableFlag) {
        BasePositionType basePositionType = new BasePositionType();
        basePositionType.setId(id);
        basePositionType.setEnableFlag(enableFlag);
        basePositionType.setUpdateDateStr(DateUtil.updateDateStr());
        basePositionType.setUpdateBy(UserUtil.getUpdateByName());
        basePositionType.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = basePositionTypeDao.updateByPrimaryKeySelective(basePositionType);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 批量禁用启用
     *
     * @param batchEnableFlagRequest
     * @return
     */
    public JsonResult batchEnableFlag(BatchEnableFlagRequest batchEnableFlagRequest) {
        JsonResult jsonResult = new JsonResult();
        //岗位类型ID
        List<String> ids = batchEnableFlagRequest.getIds();
        int count = basePositionTypeDao.batchEnableFlag(batchEnableFlagRequest.getEnableFlag(),
                batchEnableFlagRequest.getIds(),
                DateUtil.updateDateStr(),
                UserUtil.getEmployeeNo(),UserUtil.getName());
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }

        return jsonResult;
    }

}
